var o = getApp(), e = require("../../nitro.js");

Page({
    data: {
        isIpx: o.globalData.isIpx,
        aggreed: !1
    },
    checkboxChange: function(o) {
        console.info("checkboxChange", o), this.setData({
            aggreed: !this.data.aggreed
        });
    },
    onLoad: function(e) {
        console.log("app.globalData", o.globalData);
        var n = o.globalData.cart, i = [];
        n.items.forEach(function(o) {
            i.push({
                type: o.type,
                item_id: o.id,
                quantity: 1
            });
        });
        var a = {
            store_id: o.globalData.cart.storeId,
            inventory_id: n.inventory.id,
            inventory_version: n.inventory.version,
            shipping_address: {
                address: n.info.shipping_address.address || "address",
                recipient: n.info.shipping_address.recipient || "recipient",
                mobile: n.info.shipping_address.mobile || "mobile"
            },
            children: n.info.children,
            order_items: i,
            coupon: null,
            email: n.info.contact.email,
            mobile: n.info.contact.mobile,
            name: n.info.contact.name,
            extra: n.info.extra
        };
        o.globalData.cart.items.find(function(o) {
            return "5bc1f4dd9e6d245925b9d4f6" === o.category.id;
        }) && (a.coupon = {
            id: "nicolife-new-release-discounts",
            title: "新品上市85折",
            type: "by-ratio",
            scope: "service",
            discount: .85
        }), console.info("create order", a), this.setData({
            cart: o.globalData.cart,
            order: a,
            campaign: o.globalData.campaign || !1
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    onSubmit: function(o) {
        console.info("onSubmit", o.detail), wx.showLoading({
            title: "创建订单…",
            mask: !0
        }), this.onCreateOrder(o.detail.formId);
    },
    formSubmit: function(o) {
        console.log("form发生了submit事件，携带数据为：", o.detail.value);
    },
    formReset: function() {
        console.log("form发生了reset事件");
    },
    onCreateOrder: function(n) {
        return n && (this.data.order.extra.wxFormId = n), o.postRequest(o.globalData.BASE_URL + "/orders", this.data.order).then(function(n) {
            console.info("order", n), e.payOrder(n._id, function() {
                if ("nicokids-6th-groupon" === n.extra.campaign_id) {
                    var e = {
                        store_id: o.globalData.store_id,
                        order_id: n._id,
                        size: o.globalData.groupon_size,
                        campaign: "nicokids-6th-groupon"
                    };
                    o.postRequest(o.globalData.BASE_URL + "/groupons", e).then(function(o) {
                        console.info("groupon", e, o), wx.navigateTo({
                            url: "/pages/6th-groupon-created/index?groupon_id=" + o._id
                        });
                    });
                } else wx.reLaunch({
                    url: "/pages/orders/orders?reload=true"
                });
            }, function() {}), o.globalData.campaign && o.campaignEventLog("create_order");
        }, function(o) {
            console.error("err", o), wx.showToast({
                title: o.message,
                icon: "none",
                duration: 2e3,
                success: function() {}
            });
        });
    }
});